package com.dragon.logical.service.wechat.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.auth.UploadFileService;
import com.dragon.logical.service.wechat.IWechatArticleService;
import com.dragon.logical.service.wechat.WechatUtilService;
import com.dragon.repository.entity.WechatArticle;
import com.dragon.repository.mapper.WechatArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 微信文章发布表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-11-30
 */
@Service
public class WechatArticleServiceImpl extends ServiceImpl<WechatArticleMapper, WechatArticle> implements IWechatArticleService {


    @Autowired
    WechatArticleMapper mapper;

    @Autowired
    WechatUtilService wechatUtilService;

    @Autowired
    UploadFileService uploadFileService;


    @Override
    public Page<WechatArticle> selectArticle(String title, Integer status, Integer page, Integer limit) {

        Page<WechatArticle> articlePage = new Page<>(page, limit);

        LambdaQueryWrapper<WechatArticle> lambdaQueryWrapper = new LambdaQueryWrapper();

        lambdaQueryWrapper
                .eq(status != null, WechatArticle::getStatus, status)
                .like(StrUtil.isNotBlank(title), WechatArticle::getTitle, title);

        Page<WechatArticle> list = mapper.selectPage(articlePage, lambdaQueryWrapper);

        return list;
    }

    @Override
    public WechatArticle getArticleOne(Integer id) {
        WechatArticle wechatArticle = mapper.selectById(id);
        if (wechatArticle == null) {
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        return wechatArticle;
    }


    @Override
    public void addArticle(WechatArticle wechatArticle) {
        //调用wechat接口获取media_id
        String mediaId = wechatUtilService.addDart(wechatArticle);
        if (StrUtil.isBlank(mediaId)) {
            throw new CustomException(ResultCode.WECHAT_ADD_ARTICLE_ERROR);
        }
        wechatArticle.setMediaId(mediaId);
        wechatArticle.setCreateTime(DateUtil.date());
        mapper.insert(wechatArticle);
    }


    @Override
    public void updateArticle(WechatArticle wechatArticle) {
        if (!wechatUtilService.updateDart(wechatArticle)) {
            throw new CustomException(ResultCode.WECHAT_UPDATE_ARTICLE_ERROR);
        }
        wechatArticle.setUpdateTime(DateUtil.date());
        mapper.updateById(wechatArticle);
    }


    @Override
    public void deleteArticle(Integer id) {
        WechatArticle wechatArticle = mapper.selectById(id);
        if (wechatArticle == null) {
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }

        if (!wechatUtilService.deleteDart(wechatArticle.getMediaId())) {
            throw new CustomException(ResultCode.WECHAT_DELETE_ARTICLE_ERROR);
        }
        mapper.deleteById(id);
    }


    @Override
    public void releaseArticle(Integer id) {
        WechatArticle wechatArticle = mapper.selectById(id);
        if (wechatArticle == null) {
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }

        String publishId = wechatUtilService.releaseArticle(wechatArticle.getMediaId());

        if (StrUtil.isNotBlank(publishId)) {
            throw new CustomException(ResultCode.WECHAT_SUMMIT_ARTICLE_ERROR);
        }

        wechatArticle.setPublishId(publishId);
        wechatArticle.setStatus(1);
        wechatArticle.setUpdateTime(DateUtil.date());
        mapper.updateById(wechatArticle);

    }


    @Override
    public String getArticleStatus(Integer id, String publishId) {
        WechatArticle wechatArticle = mapper.selectById(id);
        if (wechatArticle == null) {
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        String publishStatus = wechatUtilService.getArticleStatus(publishId);

        if (StrUtil.isBlank(publishStatus)) {
            throw new CustomException(ResultCode.WECHAT_GET_STATUS_ERROR);
        }

        if (!publishStatus.equals("1")) {
            wechatArticle.setStatus(1);
            wechatArticle.setUpdateTime(DateUtil.date());
            mapper.updateById(wechatArticle);
        }

        return publishStatus;
    }


    @Override
    public Map<String, String> uploadFile(MultipartFile file) throws IOException {
        String path = uploadFileService.saveLocalImage(file);
        Map<String,String> result = wechatUtilService.uploadFile(path,"image");
        return result;
    }


}
